package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CurrentTypeBean;
import com.sskj.lib.bean.ShiftBean;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.presenter.StateTransferPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/07/16
 */
@Route(path = RConfig.WALLET_ACTIVITY_TRANSFER_STATE)
public class StateTransferActivity extends BaseActivity<StateTransferPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.iv_coin)
    ImageView ivCoin;
    @BindView(R2.id.tv_coin_name)
    TextView tvCoinName;
    @BindView(R2.id.tv_coin_usable)
    TextView tvCoinUsable;
    @BindView(R2.id.tv_choose_coin)
    TextView tvChooseCoin;
    @BindView(R2.id.rl_choose)
    RelativeLayout rlChoose;
    @BindView(R2.id.et_mobile)
    EditText etMobile;
    @BindView(R2.id.et_amount)
    EditText etAmount;
    @BindView(R2.id.tv_all)
    TextView tvAll;
    @BindView(R2.id.btn_confirm)
    Button btnConfirm;

    private List<String> coins = new ArrayList<>();
    private Map<String, Integer> coinIcons;
    private String usable;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_state_transfer;
    }

    @Override
    public StateTransferPresenter getPresenter() {
        return new StateTransferPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_huanzhang));
        ivRight.setImageResource(R.mipmap.icon_jilu);
        ivRight.setVisibility(View.VISIBLE);
        etAmount.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(8)});
        initCoinIcons();
    }

    private void initCoinIcons() {
        coinIcons = new HashMap<>();
        coinIcons.put(Constans.COIN_AICASH, R.mipmap.icon_ai);
        coinIcons.put(Constans.COIN_BTC, R.mipmap.icon_btc);
        coinIcons.put(Constans.COIN_ETH, R.mipmap.icon_eth);
        coinIcons.put(Constans.COIN_LTC, R.mipmap.icon_ltc);
        coinIcons.put(Constans.COIN_USDT, R.mipmap.icon_usdt);
    }

    @Override
    public void initData() {
        mPresenter.getCoins();
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(300, ivRight, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_STATE_TRANSFER).navigation();
        });
        ClickUtil.click(300, tvAll, () -> {
            if (isEmpty(usable)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_huoqukeyongxinxishibai));
                return;
            }
            etAmount.setText(usable);
        });
        ClickUtil.click(300, rlChoose, () -> {
            if (coins == null || coins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_huoqubizhongxinxishibai));
                return;
            }
            BottomDialog.chooseCoins(this, coins, this::updateCoinMsg);
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(tvCoinName)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_huoqubizhongxinxishibai));
                return;
            }
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.qignshuruduifangshoujihaoma));
                return;
            }
            if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_qingshuruzhengquedeshoujihao));
                return;
            }
            if (isEmpty(etAmount)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.qingshurushuliang));
                return;
            }
            BottomDialog.posswordInput(this, (tv) -> {
                mPresenter.stateTransfer(getText(tvCoinName).toLowerCase(), getText(etMobile), getText(etAmount), tv);
            });
        });
    }

    public void getCoinsOK(List<String> data) {
        if (data == null) {
            return;
        }
        coins.addAll(data);
        if (coins.size() > 0) {
            updateCoinMsg(coins.get(0));
        }
    }

    private void updateCoinMsg(String coinName) {
        tvCoinName.setText(coinName);
        tvChooseCoin.setText(coinName);
        ivCoin.setImageResource(coinIcons.get(coinName));
        mPresenter.getCoinUsable(coinName.toLowerCase(), "3");
    }

    public void getCoinUsableOK(ShiftBean data) {
        if (data == null) {
            return;
        }
        usable = CommonUtil.dealCoinNum(data.getPrice(), getText(tvCoinName));
        tvCoinUsable.setText(App.INSTANCE.getString(R.string.wallet_keyong)
                + CommonUtil.dealCoinNum(data.getPrice(), getText(tvCoinName))
                + getText(tvCoinName));
    }

    public void transferOK() {
        ToastUtils.show(App.INSTANCE.getString(R.string.wallet_zhuanzhengchenggong));
        ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_STATE_TRANSFER).navigation();
    }
}
